<template>
  <div>
    <el-card>
      <template #header>
        <div class="flex justify-between items-center">
          <span class="text-lg font-bold">开单参数设置</span>
          <div>
            <el-button v-if="!editMode" type="primary" @click="enableEdit">编辑</el-button>
            <el-button v-else type="success" @click="submitForm">保存</el-button>
            <el-button v-if="editMode" @click="resetForm">撤销</el-button>
          </div>
        </div>
      </template>

      <el-form :model="form" label-width="220px" :disabled="!editMode">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="系统" name="system">
            <el-form-item label="订单保存后自动审核">
              <el-switch v-model="form.autoApproveOrder" />
            </el-form-item>
            <el-form-item label="订单在审核状态下可直接修改">
              <el-switch v-model="form.allowEditApprovedOrder" />
            </el-form-item>
            <el-form-item label="系统数据备份路径">
              <el-input v-model="form.dataBackupPath" />
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="生产" name="production">
            <el-form-item label="排产提醒默认到交期天数">
              <el-input-number v-model="form.defaultScheduleWarningDays" :min="0" />
            </el-form-item>
            <el-form-item label="算料规则">
              <el-select v-model="form.materialCalculateRule" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="是否按模切规格计算材料">
              <el-switch v-model="form.useDieCutSpecForMaterialCalc" />
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="采购" name="purchase">
            <el-form-item label="供应商报价策略">
              <el-select v-model="form.supplierQuotePolicy" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_SUPPLIER_OFFER)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="净板约宽最小值">
              <el-input-number v-model="form.minBoardWidth" :min="0" />
            </el-form-item>
            <el-form-item label="原料采购是否按供应商">
              <el-switch v-model="form.directSupplierRawMaterial" />
            </el-form-item>
            <el-form-item label="辅料采购是否按供应商">
              <el-switch v-model="form.directSupplierAuxiliaryMaterial" />
            </el-form-item>
            <el-form-item label="纸板采购默认交货天数">
              <el-input-number v-model="form.defaultPaperboardDeliveryDays" :min="0" />
            </el-form-item>
            <el-form-item label="采购单价为0不允许保存">
              <el-switch v-model="form.preventZeroPriceOrder" />
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="销售" name="sale">
            <el-form-item label="客户报价策略">
              <el-select v-model="form.customerQuotePolicy" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_SUPPLIER_OFFER)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="纸箱订单默认交货天数">
              <el-input-number v-model="form.defaultDeliveryDaysBox" :min="0" />
            </el-form-item>
            <el-form-item label="彩印订单默认交货天数">
              <el-input-number v-model="form.defaultDeliveryDaysColorBox" :min="0" />
            </el-form-item>
            <el-form-item label="销售订单保存后自动审核">
              <el-switch v-model="form.autoApproveSalesOrder" />
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="仓库" name="warehouse">
            <el-form-item label="不入库可以开送货单">
              <el-switch v-model="form.allowNonStockDelivery" />
            </el-form-item>
            <el-form-item label="送货需按排程发货">
              <el-switch v-model="form.enableSchedulingDelivery" />
            </el-form-item>
            <el-form-item label="送货单车量超容量提醒">
              <el-switch v-model="form.carOvercapacityWarning" />
            </el-form-item>
            <el-form-item label="系统月结日">
              <el-input-number v-model="form.systemMonthEnd" :min="1" :max="31" />
            </el-form-item>
            <el-form-item label="原料入库限制">
              <el-select v-model="form.rawMaterialInLimit" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_MATERIAL_WARE_IN_LIMIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="成品入库限制">
              <el-select v-model="form.productInLimit" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_WARE_IN_LIMIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="成品送货提示">
              <el-select v-model="form.productDeliveryHint" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_DELIVERY_ALERT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="产品默认单位">
              <el-select v-model="form.defaultProductUnit" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="送货单保存后自动审核">
              <el-switch v-model="form.deliveryAutoAudit" />
            </el-form-item>
            <el-form-item label="成品管理模式">
              <el-select v-model="form.productManagementMode" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_MANAGE_MODE)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="成品送货方式">
              <el-select v-model="form.productDeliveryMode" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_PRODUCT_DELIVERY_MODE)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="送货单自动领用库存或生成成品">
              <el-switch v-model="form.deliveryAutoPickOrProduce" />
            </el-form-item>
            <el-form-item label="辅料入库限制">
              <el-select v-model="form.auxiliaryMaterialInLimit" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_ACCESSORIES_MATERIAL_IN_LIMIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="财务" name="finance">
            <el-form-item label="毛利分析成本">
              <el-select v-model="form.profitAnalysisBase" placeholder="请选择">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BASEORDERCONFIG_FINANCE_COST_ANALYSIS)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="启用财务结账">
              <el-switch v-model="form.enableFinancialClosing" />
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="外协" name="outsourcing">
            <el-form-item label="外协核价规格单位">
              <el-input v-model="form.outsourcingPricingUnit" />
            </el-form-item>
            <el-form-item label="外协采购数量是否取上一道工序的完工数">
              <el-switch v-model="form.outsourcingPurchaseQtyUseLastProcess" />
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { BaseOrderConfigApi } from '@/api/erp/baseorderconfig'
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'

const editMode = ref(false)
const activeTab = ref('production')

const form = reactive({
  id: '',
  autoApproveOrder: false,
  allowEditApprovedOrder: false,
  dataBackupPath: '',
  defaultScheduleWarningDays: 0,
  materialCalculateRule: '',
  useDieCutSpecForMaterialCalc: false,
  supplierQuotePolicy: '',
  minBoardWidth: 0,
  directSupplierRawMaterial: false,
  directSupplierAuxiliaryMaterial: false,
  defaultPaperboardDeliveryDays: 0,
  preventZeroPriceOrder: false,
  customerQuotePolicy: '',
  defaultDeliveryDaysBox: 0,
  defaultDeliveryDaysColorBox: 0,
  autoApproveSalesOrder: false,
  allowNonStockDelivery: false,
  enableSchedulingDelivery: false,
  carOvercapacityWarning: false,
  systemMonthEnd: 1,
  rawMaterialInLimit: '',
  productInLimit: '',
  productDeliveryHint: '',
  defaultProductUnit: '',
  productInLimitTwo: '',
  deliveryAutoAudit: false,
  productManagementMode: '',
  productDeliveryMode: '',
  deliveryAutoPickOrProduce: false,
  auxiliaryMaterialInLimit: '',
  profitAnalysisBase: '',
  enableFinancialClosing: false,
  financialClosingDate: '',
  outsourcingPricingUnit: '',
  outsourcingPurchaseQtyUseLastProcess: false,
  autoPrintProductLabel: false,
  preProductionMaintenance: false,
  touchscreenAutoPick: false,
  touchscreenAutoPrintCount: false,
  touchscreenOutsourceSkipStorage: false,
  deliveryOrderProductType: ''
})

const loadData = async () => {
  try {
    const res = await BaseOrderConfigApi.getBaseOrderConfig()
    Object.assign(form, res)
  } catch (err) {
    ElMessage.error('加载配置失败')
  }
}

const submitForm = async () => {
  try {
    await ElMessageBox.confirm('确定保存修改吗？', '提示', {
      type: 'warning'
    })
	const data = form as unknown as BaseOrderConfigVO
    await BaseOrderConfigApi.updateBaseOrderConfig(data)
    ElMessage.success('保存成功')
    editMode.value = false
  } catch (err) {
    ElMessage.error('保存失败')
  }
}

const enableEdit = () => {
  editMode.value = true
}

const resetForm = () => {
  ElMessageBox.confirm('确定撤销所有修改并恢复原始配置？', '提示', {
    type: 'warning'
  }).then(() => {
    loadData()
    editMode.value = false
  })
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.form {
  max-width: 800px;
}
</style>